package ru.spb.osll.myfit.server.execute;

import static org.junit.Assert.assertEquals;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;

import org.junit.Test;

public class TestResultMetadataDaoTest {
  
  private static final String XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 
                                    "<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">\n" + 
                                    "<properties>\n" + 
                                    "<entry key=\"TestRunComment\">offical regression\nlarge test run</entry>\n" + 
                                    "<entry key=\"foo\">bar</entry>\n" + 
                                    "</properties>\n";
 
  @Test
  public void testLoad() throws IOException {
    TestResultMetadataDao dao = new TestResultMetadataDao();
    ByteArrayInputStream stream = new ByteArrayInputStream(XML.getBytes());
    Map<String, String> metadata = dao.load(stream);
    assertEquals(2, metadata.size());
    assertEquals("bar", metadata.get("foo"));
    assertEquals("offical regression\nlarge test run", metadata.get("TestRunComment"));
  }
  
  @Test
  public void testSave() throws IOException {
    TestResultMetadataDao dao = new TestResultMetadataDao();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    TreeMap<String, String> metadata = new TreeMap<String, String>();
    metadata.put("TestRunComment", "barr");
    metadata.put("foo", "offical regression\nlarge test run");
    dao.save(metadata, out);
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    Map<String, String> storedMetadata = dao.load(in);
    assertEquals(2, storedMetadata.size());
    assertEquals("barr", storedMetadata.get("TestRunComment"));
    assertEquals("offical regression\nlarge test run", storedMetadata.get("foo"));
  }
}
